package com.yanqu.road.server.cmd.union.cross.huaben;

import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.huaben.server.UnionHuaBenPatronsData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.union.cross.UnionHuaBenModule;
import com.yanqu.road.server.logic.battle.BattleManager;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_CROSS_UNION_HUA_BEN_PATRONS_DISPATCH, desc = "门客派遣")
public class UnionHuaBenDispatchCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        UnionHuaBenProto.UnionHuaBenDispatchReqMsg reqMsg = UnionHuaBenProto.UnionHuaBenDispatchReqMsg.parseFrom(packet.getBytes());
        int idx = reqMsg.getIdx();
        List<Integer> patronsList = new ArrayList<>(reqMsg.getPatronsIdList());

        if(idx < 0 || idx > 5){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        List<UserPatrons> patrons = new ArrayList<>();
        List<Integer> patronsIdList = new ArrayList<>();
        Map<Integer, UserPatrons> userPatronsMap = player.getModule(PatronsModule.class).getUserPatronsMap();
        for (int patronsId : patronsList) {
            UserPatrons userPatrons = userPatronsMap.get(patronsId);
            if(userPatrons != null){
                patrons.add(userPatrons);
                patronsIdList.add(userPatrons.getPatronsId());
            }
        }

        if(patrons.size() == 0 || patrons.size() > GameConfig.UNION_STORY_FIGHT_GUEST_MAXOUT){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        //是否有派遣次数
        UnionHuaBenModule huaBenModule = player.getModule(UnionHuaBenModule.class);
        Map<Integer, UnionHuaBenPatronsData> patronsDataMap = huaBenModule.getPatronsDataMap();
        for (int patronsId : patronsIdList) {
            UnionHuaBenPatronsData patronsData = patronsDataMap.get(patronsId);
            if(patronsData != null){
                if(patronsData.getSendTimes() >= GameConfig.UNION_STORY_FIGHT_GUEST_OUTNUM){
                    ret = GameErrorCode.E_CROSS_UNION_HUA_BEN_PATRONS_NO_OUT_TIME;
                    return;
                }
            }
        }

        huaBenModule.addSendTime(patronsIdList);

        UnionHuaBenProto.UnionHuaBenDispatchCrossReqMsg.Builder crossReq = UnionHuaBenProto.UnionHuaBenDispatchCrossReqMsg.newBuilder();
        crossReq.setIdx(idx);
        List<UserPatrons> cloneList = BattleManager.getCloneSkillOreWarAddPatrons(patrons);
        for (UserPatrons userPatrons : cloneList) {
            UnionHuaBenProto.UnionHuaBenPatronsInfoTemp.Builder patronsTemp = UnionHuaBenProto.UnionHuaBenPatronsInfoTemp.newBuilder();
            patronsTemp.setPatronsId(userPatrons.getPatronsId());
            //门客
            patronsTemp.setAbility(userPatrons.getAbility().toString());
            PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(userPatrons.getPatronsId());
            patronsTemp.setOccupation(patronsInfo.getOccupation());
            //千分比
            //patronsTemp.setAddition(userPatrons.getOreWarBattleAddition());
            //不需要加成
            patronsTemp.setAddition(1000);
            crossReq.addPatrons(patronsTemp);
        }
        crossReq.setLevel(reqMsg.getLevel());
        player.sendPacket(Protocol.C_CROSS_UNION_HUA_BEN_PATRONS_DISPATCH, crossReq);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_CROSS_UNION_HUA_BEN_PATRONS_DISPATCH, UnionHuaBenProto.UnionHuaBenDispatchRespMsg.newBuilder().setRet(ret));
        }
    }
}
